/*
 * Date         Author     Note
 * 2015.04.28   wzwz       纱出补正
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "yarn_dmbz.h"
#include "keypane.h"
#include "info.h"


#define DMBZ_CAPTION_WIDTH   (40)
#define DMBZ_CAPTION_HEIGHT  (24)
#define DMBZ_CAPTION_INTER   (4)
#define DMBZ_CAPTION_LEFT    (CX_EDGE_LEFT * 2)
#define DMBZ_CAPTION_TOP     (CY_TITLE4_HEIGHT + CX_EDGE_LEFT)
#define DMBZ_NAME_WIDTH      (80)



/*
 *
 */
static void CreateYarnDmbzInterface(HWND hWnd)
{
      CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_DMBZ_BMP - CX_ASK_BMP - 10,
        8,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);

    CreateWindow (MH_LABEL_NAME,
        MW_BACK1_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        DMBZ_CAPTION_LEFT,
        DMBZ_CAPTION_TOP,
        DMBZ_CAPTION_WIDTH,
        DMBZ_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_BACK2_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        DMBZ_CAPTION_LEFT,
        DMBZ_CAPTION_TOP + (DMBZ_CAPTION_HEIGHT + DMBZ_CAPTION_INTER),
        DMBZ_CAPTION_WIDTH,
        DMBZ_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
       MW_BACK3_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        DMBZ_CAPTION_LEFT,
        DMBZ_CAPTION_TOP + (DMBZ_CAPTION_HEIGHT + DMBZ_CAPTION_INTER) * 2,
        DMBZ_CAPTION_WIDTH,
        DMBZ_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
       MW_BACK4_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        DMBZ_CAPTION_LEFT,
        DMBZ_CAPTION_TOP + (DMBZ_CAPTION_HEIGHT + DMBZ_CAPTION_INTER) * 3,
        DMBZ_CAPTION_WIDTH,
        DMBZ_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_FRNT1_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        DMBZ_CAPTION_LEFT,
        DMBZ_CAPTION_TOP + (DMBZ_CAPTION_HEIGHT + DMBZ_CAPTION_INTER) * 4,
        DMBZ_CAPTION_WIDTH,
        DMBZ_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_FRNT2_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        DMBZ_CAPTION_LEFT,
        DMBZ_CAPTION_TOP + (DMBZ_CAPTION_HEIGHT + DMBZ_CAPTION_INTER) * 5,
        DMBZ_CAPTION_WIDTH,
        DMBZ_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_FRNT3_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        DMBZ_CAPTION_LEFT,
        DMBZ_CAPTION_TOP + (DMBZ_CAPTION_HEIGHT + DMBZ_CAPTION_INTER) * 6,
        DMBZ_CAPTION_WIDTH,
        DMBZ_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_FRNT4_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        DMBZ_CAPTION_LEFT,
        DMBZ_CAPTION_TOP + (DMBZ_CAPTION_HEIGHT + DMBZ_CAPTION_INTER) * 7,
        DMBZ_CAPTION_WIDTH,
        DMBZ_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    int j;
    char sc[256] = {0};
    int fb[8] = {0, 0, 1, 1, 0, 0, 1, 1};
    int which[8] = {0, 1, 0, 1, 2, 3, 2, 3};
    for(j = 0; j < 8; ++j)
    {
        sprintf(sc, "%d", GetYarnDmbz(g_knit_data, fb[j], which[j]));
        CreateWindow (MH_EDIT_NAME,
            sc,
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + j,
            DMBZ_CAPTION_LEFT + DMBZ_CAPTION_WIDTH,
            DMBZ_CAPTION_TOP + (DMBZ_CAPTION_HEIGHT + DMBZ_CAPTION_INTER) * j,
            DMBZ_NAME_WIDTH,
            DMBZ_CAPTION_HEIGHT,
            hWnd, 0);
    }
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    int n = atoi(sc);
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 8)
    {
        int fb[8] = {0, 0, 1, 1, 0, 0, 1, 1};
        int which[8] = {0, 1, 0, 1, 2, 3, 2, 3};
        int ii = nID - IDC_EDIT1;
        SetYarnDmbz(g_knit_data, fb[ii], which[ii], n);
    }
    SetWindowAdditionalData(hWnd, TRUE);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID == ID_ESC)
    {
        if(GetWindowAdditionalData(hWnd))
        {
            int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
            if(ret)
            {
                printf("write dmbz: %d\n", ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }
            else
            {
                MH_FILE_SYNC();
            }
            SendSysParam(g_knit_data);
        }
        SendSysParam(g_knit_data);
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
    else if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 8)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateKeyboardWindow(hWnd, rect, -400, 400, atoi(GetWindowCaption(hTmp)));
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 7)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID > IDC_EDIT1 && nID < IDC_EDIT1 + 8)
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int YarnDmbzWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateYarnDmbzInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_DMBZ]);
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateYarnDmbzWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - CX_DMBZ_BMP) / 2;
    int y = (CY_SCREEN - CY_DMBZ_BMP) / 2;
    CreateInfo.dwStyle = WS_VISIBLE | WS_CHILD;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(614);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = YarnDmbzWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_DMBZ_BMP;
    CreateInfo.by = y + CY_DMBZ_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    else
    {
    }
    return hWnd;
}
